# -*- coding:utf-8 -*-
#! python3

from facecat import *
import os

#更新悬浮状态
#views:视图集合
def updateView(views):
	updateViewDefault(views)

#绘制视图
#view:视图
#paint:绘图对象
#clipRect:区域
def onPaint(view, paint, clipRect):
	onPaintDefault(view, paint, clipRect)

#绘制视图边线
#view:视图
#paint:绘图对象
#clipRect:区域
def onPaintBorder(view, paint, clipRect):
	onPaintBorderDefault(view, paint, clipRect)

#视图的鼠标移动方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseMove(view, mp, buttons, clicks, delta):
	onMouseMoveDefault(view, mp, buttons, clicks, delta)
		
#视图的鼠标按下方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseDown(view, mp, buttons, clicks, delta):
	onMouseDownDefault(view, mp, buttons, clicks, delta)

#视图的鼠标抬起方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseUp(view, mp, buttons, clicks, delta):
	onMouseUpDefault(view, mp, buttons, clicks, delta)

#视图的鼠标点击方法
#view 视图
#firstTouch:是否第一次触摸 
#firstPoint:第一次触摸的坐标 
#secondTouch:是否第二次触摸 
#secondPoint:第二次触摸的坐标
#clicks 点击次数
def onClick(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks):
	onClickDefault(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks)

#视图的鼠标滚动方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseWheel(view, mp, buttons, clicks, delta):
	onMouseWheelDefault(view, mp, buttons, clicks, delta)

appPaint = FCPaint() #创建绘图对象
appPaint.defaultUIStyle = "dark"
#初始化各种回调事件
appPaint.onPaint = onPaint 
appPaint.onPaintBorder = onPaintBorder
appPaint.onMouseDown = onMouseDown 
appPaint.onMouseMove = onMouseMove 
appPaint.onMouseUp = onMouseUp
appPaint.onMouseWheel = onMouseWheel
appPaint.onClick = onClick
appPaint.onUpdateView = updateView

#消息循环
def WndProc(hwnd,msg,wParam,lParam):
	return WndProcDefault(appPaint,hwnd,msg,wParam,lParam)

#创建窗体
createMainWindow(appPaint, "facecat-py", WndProc)

#创建列
#grid:表格
def createGridColumn(grid):
	gridColumn = FCGridColumn()
	if grid.paint.defaultUIStyle == "dark":
		gridColumn.backColor = "none"
		gridColumn.borderColor = "rgb(50,50,50)"
		gridColumn.textColor = "rgb(255,255,255)"
	elif grid.paint.defaultUIStyle == "light":
		gridColumn.backColor = "none"
		gridColumn.borderColor = "rgb(200,200,200)"
		gridColumn.textColor = "rgb(0,0,0)"
	gridColumn.allowResize = True
	return gridColumn
    
#创建列
#grid:表格
def createGridCell(grid):
	gridCell = FCGridCell()
	if grid.paint.defaultUIStyle == "dark":
		gridCell.backColor = "none"
		gridCell.borderColor = "rgb(50,50,50)"
		gridCell.textColor = "rgb(255,255,255)"
	elif grid.paint.defaultUIStyle == "light":
		gridCell.backColor = "rgb(255,255,255)"
		gridCell.borderColor = "rgb(200,200,200)"
		gridCell.textColor = "rgb(0,0,0)"
	return gridCell

#创建表格
grid = FCGrid()
#添加视图到框架中
addView(grid, appPaint)
#根据主题设置各种颜色
if grid.paint.defaultUIStyle == "dark":
	grid.backColor = "rgb(0,0,0)"
	grid.borderColor = "rgb(100,100,100)"
	grid.textColor = "rgb(255,255,255)"
	grid.scrollBarColor = "rgb(100,100,100)"
elif grid.paint.defaultUIStyle == "light":
	grid.backColor = "rgb(255,255,255)"
	grid.borderColor = "rgb(150,150,150)"
	grid.textColor = "rgb(0,0,0)"
	grid.scrollBarColor = "rgb(200,200,200)"
#填充满整个布局       
grid.dock = "fill"

#创建列，添加到表格中
column1 = createGridColumn(grid)
column1.text = "column1"
column1.frozen = True
column1.width = 90
grid.columns.append(column1)

column2 = createGridColumn(grid)
column2.text = "column2"
column2.width = 100
column2.frozen = True
grid.columns.append(column2)

column3 = createGridColumn(grid)
column3.text = "column3"
column3.width = 100
column3.frozen = True
grid.columns.append(column3)

column4 = createGridColumn(grid)
column4.text = "column4"
column4.width = 60
grid.columns.append(column4)

column10 = createGridColumn(grid)
column10.text = "column5"
grid.columns.append(column10)
column10.width = 140

column11 = createGridColumn(grid)
column11.text = "column6"
grid.columns.append(column11)
column11.width = 140

column5 = createGridColumn(grid)
column5.text = "column7"
grid.columns.append(column5)

column6 = createGridColumn(grid)
column6.text = "column8"
grid.columns.append(column6)

column7 = createGridColumn(grid)
column7.text = "column9"
grid.columns.append(column7)

column8 = createGridColumn(grid)
column8.text = "column10"
grid.columns.append(column8)

column9 = createGridColumn(grid)
column9.text = "column11"
grid.columns.append(column9)

#读取文件中的数据
current_file_path = __file__
current_file_dir = os.path.dirname(current_file_path)
file0 = open(current_file_dir + "\\SH600000.txt", encoding="UTF-8")
dataText = file0.read()
file0.close()
strs = dataText.split("\n")
strLen = len(strs)
for i in range(2, strLen - 1):
	subStrs = strs[i].split(",")
	if len(subStrs) > 5:
		#创建行
		row = FCGridRow()
		grid.rows.append(row)
		#创建单元格
		for c in range(0, len(grid.columns)):
			cell = createGridCell(grid)
			if c < len(subStrs):
				cell.value = subStrs[c]
			row.cells.append(cell)

#获取窗体大小
rect = win32gui.GetClientRect(appPaint.hWnd)
appPaint.size = FCSize(rect[2] - rect[0], rect[3] - rect[1])
#更新布局
updateView(appPaint.views)
showWindow(appPaint)